﻿<html>
<head>
	<title>Functions - Overloading with Number of Parameters</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
	<script type="text/javascript">

		function getCharCount(str,char, isCaseSensitive) {
			function getCharCountCaseInsensitive(char) {
				var count = 0;
				var lowerChar = char.toLowerCase();
				var upperChar = char.toUpperCase();
				for (var ch = 0; ch < str.length; ch++) {
					if (str[ch] === lowerChar || str[ch] === upperChar) count++;
				}
				return count;
			}

			function getCharCountCaseSensitive(char) {
				var count = 0;
				for (var ch = 0; ch < str.length; ch++) {
					if (str[ch] === char) count++;
				}
				return count;
			}

			switch (arguments.length) {
				case 2:
					return getCharCountCaseInsensitive(char);
				case 3:
					return isCaseSensitive ? getCharCountCaseSensitive(char) : getCharCountCaseInsensitive(char);
			}
		}															   
										   
		var str = "My name is Doncho Minkov and I am 23 years-old";

		jsConsole.writeLine("a in " + str + " - > " + getCharCount(str,'a'));
		jsConsole.writeLine("m in " + str + " - > " + getCharCount(str, 'm'));
		jsConsole.writeLine("M in " + str + " - > " + getCharCount(str, 'M'));
		jsConsole.writeLine("M in " + str + " - > " + getCharCount(str, 'M', false));
		jsConsole.writeLine("M in " + str + " - > " + getCharCount(str, 'M', true));


	</script>
</body>
</html>
